<?php
class RbacCommand extends CConsoleCommand
{
private $_authManager;
public function getHelp()
{
return <<<EOD
USAGE
rbac
DESCRIPTION
This command generates an initial RBAC authorization hierarchy.
EOD;
}

public function run($args)
{
//ensure that an authManager is defined as this is mandatory for creating an auth heirarchy
if(($this->_authManager=Yii::app()->authManager)===null)
{
echo "Error: an authorization manager, named 'authManager'
must be configured to use this command.\n";
echo "If you already added 'authManager' component in
application configuration,\n";
echo "please quit and re-enter the yiic shell.\n";
return;
}

echo "Would you like to continue? [Yes|No] ";

if(!strncasecmp(trim(fgets(STDIN)),'y',1))
{

$this->_authManager->clearAll();

$this->_authManager->createOperation("createAlumni","create alumni");
$this->_authManager->createOperation("viewAlumni","view alumni");
$this->_authManager->createOperation("updateAlumni","update alumni");
$this->_authManager->createOperation("deleteAlumni","delete alumni");


//Batch
$this->_authManager->createOperation("createBatch","create batch");
$this->_authManager->createOperation("viewBatch","view batch");
$this->_authManager->createOperation("updateBatch","update batch");
$this->_authManager->createOperation("deleteBatch","delete batch ");

//batchHistory
$this->_authManager->createOperation("createBatchHistory","create batchHistory");
$this->_authManager->createOperation("viewBatchHistory","view batchHistory");
$this->_authManager->createOperation("updateBatchHistory","update batchHistory");
$this->_authManager->createOperation("deleteBatchHistory","delete batchHistory");

//Donation
$this->_authManager->createOperation("createDonation","create donation");
$this->_authManager->createOperation("viewDonation","view donation");
$this->_authManager->createOperation("updateDonation","update donation");
$this->_authManager->createOperation("deleteDonation","delete donation");

//Cash
$this->_authManager->createOperation("createCash","create cash");
$this->_authManager->createOperation("viewCash","view cash");
$this->_authManager->createOperation("updateCash","update cash");
$this->_authManager->createOperation("deleteCash","delete cash");

//Cheque
$this->_authManager->createOperation("createCheque","create cheque");
$this->_authManager->createOperation("viewCheque","view cheque");
$this->_authManager->createOperation("updateCheque","update cheque");
$this->_authManager->createOperation("deleteCheque","delete cheque");

//Paypal
$this->_authManager->createOperation("createPaypal","create paypal");
$this->_authManager->createOperation("viewPaypal","view paypal");
$this->_authManager->createOperation("updatePaypal","update paypal");
$this->_authManager->createOperation("deletePaypal","delete paypal");

//Mission
$this->_authManager->createOperation("createMission","create mission");
$this->_authManager->createOperation("viewMission","view mission");
$this->_authManager->createOperation("updateMission","update mission");
$this->_authManager->createOperation("deleteMission","delete mission");

//MissionAssignment
$this->_authManager->createOperation("createMissionAssignment","create missionAssignment");
$this->_authManager->createOperation("viewMissionAssignment","view missionAssignment");
$this->_authManager->createOperation("updateMissionAssignment","update missionAssignment");
$this->_authManager->createOperation("deleteMissionAssignment","delete missionAssignment");

//User
$this->_authManager->createOperation("createUser","create user");
$this->_authManager->createOperation("viewUser","view user");
$this->_authManager->createOperation("updateUser","update user");
$this->_authManager->createOperation("deleteUser","delete user");


//Volunteer
$this->_authManager->createOperation("createVolunteer","create volunteer");
$this->_authManager->createOperation("viewVolunteer","view volunteer");
$this->_authManager->createOperation("updateVolunteer","update volunteer");
$this->_authManager->createOperation("deleteVolunteer","delete volunteer");

//VolunteerHistory
$this->_authManager->createOperation("createVolunteerHistory","create volunteerHistory");
$this->_authManager->createOperation("viewVolunteerHistory","view volunteerHistory");
$this->_authManager->createOperation("updateVolunteerHistory","update volunteerHistory");
$this->_authManager->createOperation("deleteVolunteerHistory","delete volunteerHistory");



$role=$this->_authManager->createRole("Admin");
$role->addChild("createAlumni");
$role->addChild("viewAlumni");
$role->addChild("updateAlumni");
$role->addChild("deleteAlumni");

$role->addChild("createBatch");
$role->addChild("viewBatch");
$role->addChild("updateBatch");
$role->addChild("deleteBatch");

$role->addChild("createBatchHistory");
$role->addChild("viewBatchHistory");
$role->addChild("updateBatchHistory");
$role->addChild("deleteBatchHistory");

$role->addChild("createDonation");
$role->addChild("viewDonation");
$role->addChild("updateDonation");
$role->addChild("deleteDonation");

$role->addChild("createCash");
$role->addChild("viewCash");
$role->addChild("updateCash");
$role->addChild("deleteCash");

$role->addChild("createCheque");
$role->addChild("viewCheque");
$role->addChild("updateCheque");
$role->addChild("deleteCheque");

$role->addChild("createPaypal");
$role->addChild("viewPaypal");
$role->addChild("updatePaypal");
$role->addChild("deletePaypal");

$role->addChild("createMission");
$role->addChild("viewMission");
$role->addChild("updateMission");
$role->addChild("deleteMission");

$role->addChild("createMissionAssignment");
$role->addChild("viewMissionAssignment");
$role->addChild("updateMissionAssignment");
$role->addChild("deleteMissionAssignment");

$role->addChild("createUser");
$role->addChild("viewUser");
$role->addChild("updateUser");
$role->addChild("deleteUser");

$role->addChild("createVolunteer");
$role->addChild("viewVolunteer");
$role->addChild("updateVolunteer");
$role->addChild("deleteVolunteer");

$role->addChild("createVolunteerHistory");
$role->addChild("viewVolunteerHistory");
$role->addChild("updateVolunteerHistory");
$role->addChild("deleteVolunteerHistory");

//Encoder Role
$role=$this->_authManager->createRole("Encoder");
$role->addChild("createAlumni");
$role->addChild("viewAlumni");
$role->addChild("updateAlumni");


$role->addChild("createBatch");
$role->addChild("viewBatch");
$role->addChild("updateBatch");
$role->addChild("deleteBatch");

$role->addChild("createBatchHistory");
$role->addChild("viewBatchHistory");
$role->addChild("updateBatchHistory");
$role->addChild("deleteBatchHistory");

$role->addChild("createDonation");
$role->addChild("viewDonation");
$role->addChild("updateDonation");
$role->addChild("deleteDonation");

$role->addChild("createCash");
$role->addChild("viewCash");
$role->addChild("updateCash");
$role->addChild("deleteCash");

$role->addChild("createCheque");
$role->addChild("viewCheque");
$role->addChild("updateCheque");
$role->addChild("deleteCheque");

$role->addChild("createPaypal");
$role->addChild("viewPaypal");
$role->addChild("updatePaypal");
$role->addChild("deletePaypal");

$role->addChild("createMission");
$role->addChild("viewMission");
$role->addChild("updateMission");
$role->addChild("deleteMission");

$role->addChild("createMissionAssignment");
$role->addChild("viewMissionAssignment");
$role->addChild("updateMissionAssignment");
$role->addChild("deleteMissionAssignment");

$role->addChild("createUser");
$role->addChild("viewUser");
$role->addChild("updateUser");
$role->addChild("deleteUser");

$role->addChild("createVolunteer");
$role->addChild("viewVolunteer");
$role->addChild("updateVolunteer");
$role->addChild("deleteVolunteer");

$role->addChild("createVolunteerHistory");
$role->addChild("viewVolunteerHistory");
$role->addChild("updateVolunteerHistory");
$role->addChild("deleteVolunteerHistory");

//Encoder Role
$role=$this->_authManager->createRole("Alumni");
$role->addChild("createAlumni");
$role->addChild("viewAlumni");
$role->addChild("updateAlumni");

$role->addChild("createDonation");
$role->addChild("viewDonation");
$role->addChild("updateDonation");
$role->addChild("deleteDonation");

$role->addChild("createCash");
$role->addChild("viewCash");
$role->addChild("updateCash");
$role->addChild("deleteCash");

$role->addChild("createCheque");
$role->addChild("viewCheque");
$role->addChild("updateCheque");
$role->addChild("deleteCheque");

$role->addChild("createPaypal");
$role->addChild("viewPaypal");
$role->addChild("updatePaypal");
$role->addChild("deletePaypal");

$role->addChild("createVolunteer");
$role->addChild("viewVolunteer");
$role->addChild("updateVolunteer");

$role->addChild("createUser");
$role->addChild("viewUser");
$role->addChild("updateUser");

$role->addChild("createMission");
$role->addChild("viewMission");
$role->addChild("updateMission");

$role->addChild("createMissionAssignment");
$role->addChild("viewMissionAssignment");
$role->addChild("updateMissionAssignment");


$role=$this->_authManager->createRole("Volunteer");
$role->addChild("createAlumni");
$role->addChild("viewAlumni");
$role->addChild("updateAlumni");

$role->addChild("createDonation");
$role->addChild("viewDonation");
$role->addChild("updateDonation");
$role->addChild("deleteDonation");

$role->addChild("createCash");
$role->addChild("viewCash");
$role->addChild("updateCash");
$role->addChild("deleteCash");

$role->addChild("createCheque");
$role->addChild("viewCheque");
$role->addChild("updateCheque");
$role->addChild("deleteCheque");

$role->addChild("createPaypal");
$role->addChild("viewPaypal");
$role->addChild("updatePaypal");
$role->addChild("deletePaypal");

$role->addChild("createVolunteer");
$role->addChild("viewVolunteer");
$role->addChild("updateVolunteer");

$role->addChild("createUser");
$role->addChild("viewUser");
$role->addChild("updateUser");

$role->addChild("createMission");
$role->addChild("viewMission");
$role->addChild("updateMission");

$role->addChild("createMissionAssignment");
$role->addChild("viewMissionAssignment");
$role->addChild("updateMissionAssignment");

echo "Authorization hierarchy successfully generated.";
}
}
}
